<template>
  <div class="box">
    <div>
      <van-search
        v-model="value"
        show-action
        shape="round"
        background="#4fc08d"
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @focus="gosearch"
      >
        <template #left>
          <div class="dizhi" @click="onClickButton2">地址</div>
        </template>
        <template #action>
          <!--根据登录状态 条件渲染 -->
          <div @click="onClickButton" v-if="!token">登录</div>
          <div @click="onClickButton1" v-if="token">我的</div>
        </template>
      </van-search>
    </div>
    <div class="swiper">
      <swiper></swiper>
    </div>
    <div class="jindong">
      
      <jingd></jingd>
    </div>
    <div>
      <div class="CountDownTime">
        <span>京东秒杀</span>
      <!-- 秒杀计时器 -->
        <span style="color: red">{{ times }}点专场</span>
        <van-count-down :time="time">
          <template #default="timeData">
            <span class="block">{{ timeData.hours }}</span>
            <span class="colon">:</span>
            <span class="block">{{ timeData.minutes }}</span>
            <span class="colon">:</span>
            <span class="block">{{ timeData.seconds }}</span>
          </template>
        </van-count-down>
      </div>
      <seckilllist></seckilllist>
    </div>
    <div>
      <list></list>
    </div>
  </div>
</template>

<script>
import swiper from "./home/swiper.vue";
import jingd from "./home/jingd.vue";
import seckilllist from "./home/seckilllist.vue";
import list from "./home/list.vue";
export default {
  data() {
    return {
      times: "",
      value: "",
      //
      token: "",

      time: 1 * 60 * 60 * 1000,
    };
  },
  methods: {
    onSearch() {},
    onClickButton() {
      this.$router.push("/login");
    },
    onClickButton1() {
      this.$router.push("/mine");
    },
    onClickButton2() {
      this.$router.push("/mine");
    },
    gosearch() {
      this.$router.push("/search");
    },
    getTimes() {
      setInterval(this.getTimesInterval, 3600000 * 1000);
    },
    getTimesInterval: function () {
      let hours = new Date().getHours(); 
      //获取当前时间的小时
      //当小于 10 的是时候，在前面加 0
      if (hours < 10) {
        hours = "0" + hours;
      }
      this.times = hours;
    },
  },
  //组件内路由控制底部导航栏是否可见
  beforeRouteLeave(to, from, next) {
      this.$store.commit("channav", false);
    next();
  },
  created() {
    console.log(this.$store.state.userInfo);
    this.token = localStorage.getItem("token");
    this.$store.commit("channav", true);
    this.getTimes();
  },
  beforeDestroy() {
    if (this.times) {
      clearInterval(this.getTimesInterval);
    }
  },
  components: {
    swiper,
    jingd,
    seckilllist,
    list,
  },
};
</script>

<style lang="scss" scoped>
.swiper {
  width: 100%;
  height: 200px;
}
.jindong {
  width: 100%;
}
.dizhi {
  font-size: 16px;
}
.CountDownTime {
      display: flex;
      justify-content: space-between;
      padding: 20px 20px;
      font-size: 16px;
      
      .colon {
        display: inline-block;
        font-size: 20px;
        padding: 0 10px;
        color: red;
      }
      .block {
        display: inline-block;
        width: 26px;
        height: 26px;
        line-height: 26px;
        color: #fff;
        font-size: 20px;
        text-align: center;
        background-color: red;
      }
    }
.box{
  width: 100%;
  overflow: hidden;
}
</style>
